#pragma once
#include <iostream>
#include<vector>
#include<functional>
using namespace std;

namespace bit

{
    template <class T, class Container = vector<T>, class Compare = less<T> >

    class priority_queue

    {

    public:

        priority_queue()
            :c(Container())
            ,comp(Compare())
        {}

        template <class InputIterator>

        priority_queue(InputIterator first, InputIterator last)
            : c(Container())
            , comp(Compare())
        {
            while (first < last)
            {
                push(*first);
                first++;
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        const T& top() const
        {
            return c[0];
        }

        void push(const T& x)
        {
            c.push_back(x);
            adjust_up(c.size() - 1);
        }

        void pop()
        {
            swap(c[0], c[c.size() - 1]);
            c.pop_back();
            adjust_down(0);
        }

    private:
        void adjust_down(size_t parent)
        {
            size_t child = parent * 2 + 1;
            while (child < c.size())
            {
                if (child + 1 < c.size() && comp(c[child + 1], c[child]))
                {
                    child++;
                }

                if (comp(c[child], c[parent]))
                {
                    swap(c[child], c[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void adjust_up(size_t child)
        {
            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (comp(c[child], c[parent]))
                {
                    swap(c[child], c[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }

        Container c;

        Compare comp;

    };

};